<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use backend\utils\XHtml;

/* @var $this yii\web\View */
/* @var $model common\models\Catalog */

$this->title = $model->isNewRecord ? '新建栏目' : '更新栏目';
$this->params['breadcrumbs'][] = ['label' => '栏目管理', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
    <div class="page-create">
        <div class="box box-primary">
            <div class="box-body">

                <?= $this->render('/site/error-summary', ['model' => $model]) ?>

                <?php $form = ActiveForm::begin([
                    'options' => ['class' => 'form-horizontal'],
                    'fieldConfig' => [
                        'template' => "{label}\n<div class=\"col-sm-7\">{input}</div>\n<div class=\"col-sm-3\">{hint}\n{error}</div>",
                        'labelOptions' => ['class ' => 'col-sm-2 control-label'],
                    ]
                ]); ?>
                <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
                <?= $form->field($model, 'us_name')->textInput(['maxlength' => true]) ?>
                <?= $form->field($model, 'alias')->textInput(['maxlength' => true]) ?>
                <?= $form->field($model, 'sort')->textInput(['type' => 'number', 'min' => 1]) ?>
                <?= $form->field($model, 'parent_id')->dropDownList($model->getTrees(), ['prompt' => '顶级分类'])->label('上级分类') ?>
                <?= $form->field($model, 'intro')->textarea() ?>
                <?= $form->field($model, 'frontend_nav')->checkbox()->label('&nbsp;') ?>
                <?= $form->field($model, 'page_size')->textInput(['type' => 'number', 'min' => 0]) ?>
                <?= $form->field($model, 'type')->dropDownList($model->typeList())->label('展现方式') ?>
                <?= $form->field($model, 'view_list')->dropDownList(\common\models\View::getDir(), ['prompt' => '请选择']) ?>
                <?= $form->field($model, 'view_show')->dropDownList(\common\models\View::getPage(), ['prompt' => '请选择']) ?>
                <?= $form->field($model, 'view_item')->dropDownList($viewItem, ['prompt' => '请选择']) ?>
                <?= $form->field($model, 'link')->textInput(['maxlength' => true]) ?>
                <div class="form-group" id="articleBox">
                    <label class="col-sm-2 control-label">素材</label>
                    <div class="col-sm-7">
                        <?= Html::input('text', 'article', $articleInfo['title'], ['id' => 'article', 'class' => 'form-control', 'readonly' => 'readonly']) ?>
                        <?= Html::input('hidden', 'article_id', $articleInfo['id'], ['id' => 'article_id']) ?>
                    </div>
                    <div class="col-sm-3">
                        <?= Html::button('选择素材', ['id' => 'articleBt', 'class' => 'btn btn-default', 'data-toggle' => 'modal', 'data-target' => '#myModal']) ?>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label">SEO</label>
                    <div class="col-sm-7">
                        <?php if ($meta->id) { ?>
                            <?= Html::button('<i class="fa fa-minus"></i>', ['id' => 'seoBt']) ?>
                        <?php } else { ?>
                            <?= Html::button('<i class="fa fa-plus"></i>', ['id' => 'seoBt']) ?>
                        <?php } ?>
                    </div>
                </div>

                <div class="box box-solid" id="seoBox" <?= $meta->id ? 'style="box-shadow:none"' : 'style="display:none;box-shadow:none"' ?>>
                    <?= $form->field($meta, 'title')->textInput(['maxlength' => true]) ?>
                    <?= $form->field($meta, 'keywords')->textInput(['maxlength' => true]) ?>
                    <?= $form->field($meta, 'description')->textInput(['maxlength' => true]) ?>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label"></label>
                    <?= XHtml::submitButton($model->isNewRecord ? '创建' : '更新') ?>&nbsp;&nbsp;
                    <?= XHtml::backButton() ?>&nbsp;&nbsp;
                    <?= XHtml::resetButton() ?>
                </div>
                <?php ActiveForm::end(); ?>
            </div>
        </div>
    </div>

    <!-- 选择素材 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="myModalLabel">
                        <div class="row">
                            <div class="col-lg-5">选择素材</div>
                            <div class="col-lg-6">
                                <div class="input-group">
                                    <?= Html::textInput('key', '', ['class' => 'form-control']) ?>
                                    <span class="input-group-btn">
                                        <?= Html::button('查询', ['class' => 'btn btn-default', 'id' => 'btSearch']) ?>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </h4>
                </div>
                <div class="modal-body" id="articleBody"></div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="articleOk">确定</button>
                </div>
            </div>
        </div>
    </div>
    <!-- 选择素材 END -->
<?php
$getArticleUrl = \yii\helpers\Url::to(['index', 'act' => 'getArticle']);
$script = <<<JS
    if ('$model->type'!='3'){
        $(".field-catalog-link").hide();
    }
    if ('$model->type'!='2'){
        $("#articleBox").hide();
    }
    if ('$model->type'!='1'&&'$model->type'!=''){
        $(".field-catalog-view_list").hide();
        $(".field-catalog-view_item").hide();
    }
    if ('$model->type'!='2'&&'$model->type'!='1'&&'$model->type'!=''){
        $(".field-catalog-view_show").hide();
    }
    $("#catalog-type").change(function() {
        if($(this).val()=='3'){
            $(".field-catalog-link").show().focus();
            $("#articleBox").hide();
            $(".field-catalog-view_list").hide();
            $(".field-catalog-view_show").hide();
            $(".field-catalog-view_item").hide();
        }else {
            if ($(this).val()=='2'){
                $(".field-catalog-link").hide();
                $(".field-catalog-view_list").hide();
                $(".field-catalog-view_item").hide();
                $(".field-catalog-view_show").show();
                $("#articleBox").show();
                if ($("#articleBody").html()==""){
                    getArticle();
                }
            }else {
                $("#catalog-link").val('');
                $(".field-catalog-link").hide();
                $(".field-catalog-view_list").show();
                $(".field-catalog-view_item").show();
                $(".field-catalog-view_show").show();
                $("#articleBox").hide();
            }
        }
    });
    $("#seoBt").click(function() {
        if ($(this).find(".fa-plus").length>0){
            $("#seoBox").slideDown();
            $(this).find("i").removeClass("fa-plus").addClass("fa-minus");
        }else {
            $("#seoBox").slideUp();
            $(this).find("i").removeClass("fa-minus").addClass("fa-plus");
        }
    });
    function getArticle(key='',page=1) {
        $.get("$getArticleUrl",{key:key,page:page},function(result) {
            $("#articleBody").html(result);
        });
    }
    $("#articleOk").click(function() {
        $('#myModal').modal('hide');
        var obj = $("#articleBody").find(".art_id:checked");
        if(obj.length>0){
            $("#article").val(obj.parent().next().next().text());
            $("#article_id").val(obj.val());
        }
    });
    $("#btSearch").click(function() {
        getArticle($("input[name=key]").val(),1);
    });
JS;
$this->registerJs($script);
?>